/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.javadoc;

import java.util.List;

import com.prefabware.jmodel.code.JCodeOptions;

/**
 * @author sisele_job
 * 
 */
public abstract class JJavadoc extends JMultiLineComment {

	private String since;

	/**
	 * @since 4
	 * @return
	 */
	public String getSince() {
		return since;
	}

	/**
	 * 
	 */
	public JJavadoc() {
		super();		
	}

	/**
	 * @param value
	 *            the
	 */
	public void setSince(String since) {
		this.since = since;
	}

	@Override
	public String asCode(JCodeOptions options) {
		StringBuffer doc = new StringBuffer();
		doc.append(BEGIN_BLOCK);
		doc.append(this.text);
		// let subclasses append their own tags
		int lengthBefore = doc.length();
		doc = appendTags(doc);
		if (this.isEmpty() && doc.length() == lengthBefore) {
			// the text was empty and the subclasses did not append nothing
			// do not produce empty javadocs
			return "";
		}
		appendTag("@since", this.since, doc);
		doc.append(END_BLOCK);
		return doc.toString();
	}

	protected abstract StringBuffer appendTags(StringBuffer doc);

	/**
	 * appends the tag at the end of the given document followed by a line feed
	 * appends nothing, if <code>tagValue == null</code>
	 * 
	 * @param tag
	 *            the name of the tag, starting with an @
	 * @param tagValue
	 *            the value of the tag. may be null
	 * @param document
	 *            the document containing the javadoc
	 * @return
	 */
	protected StringBuffer appendTag(String tag, String tagValue, StringBuffer document) {
		if (tagValue != null) {
			document.append(BEGIN_LINE).append(tag).append(" ").append(tagValue).append(LF);
		}
		return document;
	}

	/**
	 * appends the list of tags to the document
	 * 
	 * @param tag
	 * @param tagValues
	 *            the list of values
	 * @param document
	 * @return
	 */
	protected StringBuffer appendTag(String tag, List<String> tagValues, StringBuffer document) {
		if (tagValues != null && !tagValues.isEmpty()) {
			for (String tagValue : tagValues) {
				appendTag(tag, tagValue, document);
			}
		}
		return document;
	}

	@Override
	public String toString() {
		return asCode(JCodeOptions.DEFAULT);
	}
}
